Skip to main content

MongoDB API

Overview

The Qarbine driver for the MongoDB API permits either MongoDB Query Language or SQL queries. The latter is only supported when the endpoint is MongoDB Atlas SQL. Qarbine previews the query to determine which underlying endpoint service to call.

A collection in MongoDB is analogous to a table in relational databases. However, collections are more flexible. They don’t enforce a strict schema unless configured to do so. Each collection is associated with one MongoDB database.

MongoDB API databases store data as JSON/BSON documents. Each document represents a record and can have varying fields. Unlike traditional relational databases, where adding a field means altering the table structure, MongoDB allows flexible schema changes. Documents can be nested to express hierarchical relationships and store structures like arrays. Qarbine is perfectly fine with such nesting and dynamic answer sets.

The MongoDB Query Language (MQL) aggregation pipelines interface enables complex data transformations. Pipelines consist of stages (e.g., $match, $group, $project) that process data sequentially. Developers and others skilled in MQL can natively use pipelines for analytics, data flows, and custom aggregations. For more details on the various stages see https://www.mongodb.com/docs/atlas/atlas-stream-processing/stream-aggregation/.

MongoDB API databases provide many options for high availability which vary by vendor. The use of replica sets, sharding and indexes are activities “behind” the connection string. In general this functionality is transparent to Qarbine and other applications.

The MongoDB API Tutorial has additional information on using Qarbine with MongoDB API supporting databases.

MongoDB Querying Language

Qarbine supports direct, native MongoDB queries. This is the best way to access the full power of the MongoDB data platform, especially the aggregation pipeline. In general all MongoDB query constructors are supported. For mode details see
https://www.mongodb.com/docs/manual/tutorial/query-documents/

The data source below retrieves a list of portfolios.

  

Here is a query for a portfolio collection that takes advantage of the MongoDB document model features.

db.portfolio.find( {cash: {$gt: 2000}, isIRA: false } )

A sample portfolio document with embedded documents and arrays may look like

  

The query results can directly feed a Qarbine analysis template which indicates how to iterate through the documents, interact with them using formulas, and format the results. The report “just works” like you’d expect because it uses the same data model as your application! Qarbine can handle any data shape and has lots of conditional configuration as well. The query is a one liner using MongoDB Query Language. It can be super painful to even attempt to use legacy SQL tools with the document model and dynamic data structures enabled by MongoDB.
See the Tutorials area for one describing an example using a MongoDB API data source, template, and prompt.

Reviewing the Generated Specification

You can enter criteria of the form “EXPLAIN ….” to have the query processed and have the returned answer set be the underlying final query specification. A convenient way of specifying this is to have “explain” on the first line and the rest of your SQL on the next lines.

explain
db.foo.find(...)

Then simply “comment out” the first line when not in use

// explain
db.foo.find(...)

Another way to get the specification is to press ALT and click   . Any “explain SELECT” or “explain: true” takes precedence over the ALT-click interaction.

Vector Querying Prerequisites

Prior to using Qarbine’s embeddings(...) macro function the Qarbine Administrator must first configure “AI Assistant(s)”. The AI Assistants provide access to various popular Generative AI services and are referenced using an alias. Check with your Qarbine administrator for which ones are available and their proper use. For example, when using dynamic query vector embeddings, the model used by the AI Assistant must be compatible with the one used to generate the original embedding values in the database.

MongoDB Atlas™ SQL Querying

The Qarbine integration with MongoDB also supports the MongoDB Atlas SQL interface as well. Simply enter the Atlas SQL statement and click “Run”. Qarbine recognizes the query as being SQL oriented and calls the appropriate MongoDB API.

For information on the MongoDB Atlas SQL interactions see https://www.mongodb.com/docs/atlas/data-federation/query/sql/language-reference/

Qarbine Virtual Queries

There are a few convenience queries which are mainly DBA oriented. These queries are recognized by the Qarbine driver and provide common database information.

Query Description
list databasesReturn a list of databases.
list collectionsReturn a list of Couchbase collections.

See the “DBA Productivity” section of the online documentation for more details.